﻿using Model;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Data.Service;

namespace SYT.AliApiHandle
{
    public class GetToken
    {
        //获取授权码方法。
        public static string GetCodeSkipMyUrl()
        {
            string client_id = System.Configuration.ConfigurationManager.AppSettings["Appkey"].ToString();
            string client_secret = System.Configuration.ConfigurationManager.AppSettings["AppSecret"].ToString();

            //请求Code授权回调页面参数。
            string Url = "https://www.baidu.com";
            //string Url = "www.cctvuuu.com/Home/AlRequstCode";
            //string Url = "http://syzs.0755sdl.com/Home/AlRequstCode";
            Dictionary<string, string> dc = new Dictionary<string, string>();
            dc.Add("client_id", client_id);
            dc.Add("site", "china");
            dc.Add("redirect_uri", Url);
            string sign = ApiHelp.dengsign(dc);
            string urlstr = "http://gw.open.1688.com/auth/authorize.htm?client_id=" + client_id + "&site=china&redirect_uri=" + Url + "&_aop_signature=" + sign;
            //这里返回了临时授权码。
            return urlstr;
        }

        //使用授权码获取令牌
        public static string getTokenFunc(string code, out bool isNOone)
        {
            string client_id = System.Configuration.ConfigurationManager.AppSettings["Appkey"].ToString();
            string client_secret = System.Configuration.ConfigurationManager.AppSettings["AppSecret"].ToString();

            string urlStr = "https://gw.open.1688.com/openapi/http/1/system.oauth2/getToken/" + client_id + "";
            Dictionary<string, string> dc = new Dictionary<string, string>();
            dc.Add("grant_type", "authorization_code");
            dc.Add("need_refresh_token", "true");
            dc.Add("client_id", client_id);
            dc.Add("client_secret", client_secret);
            dc.Add("redirect_uri", "http://syzs.0755sdl.com");
            dc.Add("code", code);


            string Token = ApiHelp.DoPost(urlStr, dc);

            Hashtable hs = (Hashtable)PluSoft.Utils.JSON.Decode(Token); ;


            //把令牌插入数据库或更新数据库.
            string aliId = hs["aliId"].ToString();
            string memberId = hs["memberId"].ToString();
            string refresh_token_timeout = hs["refresh_token_timeout"].ToString();
            string resource_owner = hs["resource_owner"].ToString();
            string access_token = hs["access_token"].ToString();
            string refresh_token = hs["refresh_token"].ToString();

            //更新当前用户信息到数据库，返回当前用户对象。
            Users user = UsersData.UpdateUserAllbbInfo(aliId, memberId, refresh_token_timeout, resource_owner, access_token, refresh_token, out isNOone);

            //自动更新用户各项产品数量信息。
            //ProductManage.AutoUpdateProCountInfo(user.aliId,user.memberId,user.access_token);

            return JsonConvert.SerializeObject(user);

        }

        //是使用长效临牌获取短期令牌。
        public static string ExchangeAccessToken(string refresh_token, out bool isNOone)
        {
            string client_id = System.Configuration.ConfigurationManager.AppSettings["Appkey"].ToString();
            string client_secret = System.Configuration.ConfigurationManager.AppSettings["AppSecret"].ToString();


            string api = "https://gw.api.alibaba.com/openapi/param2/1/system.oauth2/getToken/" + client_id + "";
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add("grant_type", "refresh_token");
            dic.Add("client_id", client_id);
            dic.Add("client_secret", client_secret);
            dic.Add("refresh_token", refresh_token);
            string result = ApiHelp.DoPost(api, dic);
            Hashtable hs = (Hashtable)PluSoft.Utils.JSON.Decode(result);

            //把令牌插入数据库或更新数据库.
            string aliId = hs["aliId"].ToString();
            string memberId = hs["memberId"].ToString();
            string refresh_token_timeout = hs["refresh_token_timeout"] != null ? hs["refresh_token_timeout"].ToString() : "NG";
            string resource_owner = hs["resource_owner"].ToString();
            string access_token = hs["access_token"].ToString();
            string refreshs_token = refresh_token;

            //更新当前用户信息到数据库，返回当前用户对象。
            Users user = UsersData.UpdateUserAllbbInfo(aliId, memberId, refresh_token_timeout, resource_owner, access_token, refreshs_token, out isNOone);
            return access_token;
        }
    }
}
